home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™ 1987-1994 / MacHack™ '90 / MacHack 90 Contest Entries / Surovell Stuffƒ / Pollyƒ / PolyUtils.c next >
Encoding:
C/C++ Source or Header  |  1990-06-15  |  3.8 KB  |  171 lines  |  [TEXT/KAHL]

  1. /*
  2.     Copyright © 1990 by Succinct Systems
  3.  
  4.     433 Huronview
  5.     Ann Arbor, MI 48103
  6.     (313) 663-4903
  7.  
  8.     File:         PolyUtils.c
  9.     Model:         THINK C 4.0, MPW C 3.0
  10.  
  11.     ABSTRACT:
  12.         generate regular polygons and lists of endpoints
  13.  
  14.     NOTES:
  15.         none yet.
  16.  
  17.     KNOWN BUGS:
  18.         none yet.
  19.  
  20.     HISTORY:
  21.         DAS 10-Jun-90    created this file    (David A. Surovell)
  22. */
  23.  
  24. #include <StdMacIncludes.h>                /* include standard Mac headers */
  25. #include <Compatibility.h>                /* define compatibility control structures */
  26. #include <SANE.h>
  27. #include "PolyUtils.h"
  28.  
  29.  
  30. /*
  31. **    create a regular polygon;
  32. **    center point is ( vertexRadius, vertexRadius ).
  33. **
  34. **        arguments:
  35. **            thisPoly:        polygonal result
  36. **            sideCount:        number of sides
  37. **            vertexRadius:    length from center to vertex
  38. **            degreeOffset:    start and this number of degrees from 0°
  39. **
  40. **        return:
  41. **            noErr:        success
  42. **            -1:            bad arguments
  43. **            (other):    MemoryMgr error
  44. */
  45. OSErr CreateRegularPoly(
  46.     PolyHandle    *thisPoly,
  47.     short        sideCount,
  48.     short        vertexRadius,
  49.     short        degreeOffset )
  50. {
  51. PolyHandle    localPH;
  52. Point        *vertices;
  53. short        i;
  54. OSErr        errStat;
  55.  
  56.     if (thisPoly == NULL)
  57.         return (OSErr)(-1);
  58.  
  59.     *thisPoly = NULL;
  60.  
  61.     /* obtain the list of polygon endpoints */
  62.     errStat = CreateVertexList( &vertices, sideCount, vertexRadius, degreeOffset );
  63.     if (errStat != noErr)
  64.         return errStat;
  65.  
  66.     /* create the polygon itself */
  67.     localPH = OpenPoly();
  68.         MoveTo( vertices[0].h, vertices[0].v );
  69.         for (i=1; i<sideCount; i++)
  70.             LineTo( vertices[i].h, vertices[i].v );
  71.  
  72.         LineTo( vertices[0].h, vertices[0].v );
  73.     ClosePoly();
  74.  
  75.     /* cleanup and report */
  76.     DisposPtr( vertices );
  77.     if (errStat == noErr)
  78.         *thisPoly = localPH;
  79.  
  80.     return errStat;
  81. }
  82.  
  83.  
  84. /*
  85. **    calculate a list of endpoints for a regular polygon;
  86. **    center point is ( vertexRadius, vertexRadius ).
  87. **
  88. **        arguments:
  89. **            thisPoly:        polygonal result
  90. **            sideCount:        number of sides
  91. **            vertexRadius:    length from center to vertex
  92. **            degreeOffset:    start and this number of degrees from 0°
  93. **
  94. **        return:
  95. **            noErr:        success
  96. **            -1:            bad arguments
  97. **            (other):    MemoryMgr error
  98. */
  99. OSErr CreateVertexList(
  100.     PointPtr    *pointList,
  101.     short        sideCount,
  102.     short        vertexRadius,
  103.     short        degreeOffset )
  104. {
  105. register Point    *vertices;
  106. register short    i;
  107. register Fixed    trigFixer, radiusFixer;
  108. register OSErr    errStat;
  109.  
  110. #ifdef _MC68881_
  111. extended    curFloatCoord;
  112. double        curDegrees, curRadians;
  113. #else
  114. extended    curFloatCoord, curDegrees, curRadians;
  115. #endif _MC68881_
  116.  
  117.     if ((pointList == NULL) || (sideCount < 3) || (vertexRadius < sideCount))
  118.         return (OSErr)(-1);
  119.  
  120.     /* force “degreeOffset” into sensible range */
  121.     while (degreeOffset < 0)
  122.         degreeOffset += 360;
  123.     degreeOffset %= 360;
  124.  
  125.     pointList = NULL;
  126.     vertices = (Point*)NewPtr( sideCount * sizeof(Point) );
  127.     errStat = MemError();
  128.     if (errStat != noErr)
  129.         return errStat;
  130.  
  131.     /* create the a list of vertex points */
  132.     radiusFixer = (long)vertexRadius << 16;
  133.     for (i=0; i<sideCount; i++)
  134.     {
  135.         /* calculate the degree position of the endpoint, */
  136.         /* and convert it into radians */
  137.         curDegrees = degreeOffset + ((i * 360.0) / (sideCount * 1.0));
  138.         curRadians = (curDegrees * 3.14159265) / 180.0;
  139.  
  140.         /* determine the x coordinate */
  141. #ifdef _MC68881_
  142.         x96tox80( &curRadians, &curFloatCoord );
  143.         curFloatCoord = cos( curFloatCoord );
  144. #else
  145.         curFloatCoord = cos( curRadians );
  146. #endif _MC68881_
  147.  
  148.         trigFixer = X2Fix( &curFloatCoord );
  149.         vertices[i].h =
  150.             vertexRadius + FixRound( FixMul( radiusFixer, trigFixer ) );
  151.  
  152.         /* determine the y coordinate */
  153.         /* compensate for the “upside-downness” of the video coordinate system */
  154. #ifdef _MC68881_
  155.         x96tox80( &curRadians, &curFloatCoord );
  156.         curFloatCoord = sin( curFloatCoord );
  157. #else
  158.         curFloatCoord = sin( curRadians );
  159. #endif _MC68881_
  160.  
  161.         trigFixer = X2Fix( &curFloatCoord );
  162.         vertices[i].v =
  163.             vertexRadius - FixRound( FixMul( radiusFixer, trigFixer ) );
  164.     }
  165.  
  166.     *pointList = vertices;
  167.  
  168.     return errStat;
  169. }
  170.  
  171.